/*
 * 客户端实现类。
 * Copyright (C) 2009  Xubin
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.sysmo.client;

import org.sysmo.client.item.Item;
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.util.HashMap;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.sysmo.communication.CommunicationInterface;
import org.w3c.dom.*;
import org.xml.sax.SAXException;

/**
 *客户端类实现.用户登录一台客户端时, 客户端读取本地配置文件. 配置文件中保存客户端ID,
 * 客户端名字; 服务器地址, 端口以及需要监控的项(Item), 对应的监控项ID和对应数据采集
 * 间隔. 读取过程中客户端为每个监控项创建一个监控项类实例, 然后调用Item类的start()方
 * 法. 读取完毕后客户端尝试连接服务器, 建立CommunicationInterface实例, 然后调用
 * serverListener()方法监听服务器消息.
 * @author Xubin
 */
public class Client {

    private int clientID;
    private char clientName;
    private HashMap<Integer, Item> itemMap;

    private InetAddress serverAddress;
    private int serverPort;
    private String signFile;
    private boolean serverConnected;
    private CommunicationInterface communication;

    boolean shutDownFlag;


    /**
     *构造函数, 启动客户端通过外部调用start()方法实现
     *
     */
    public  Client(){

    }

    private void readConfig(){
        
    }

    private void saveConfig(){
        /*save client instance parameter to clientconfig.xml*/

    }

    private void connectServer(){
        boolean connected = false;
        communication = new CommunicationInterface(signFile, serverAddress, serverPort);
        serverConnected = communication.tryConnect();
        if(serverConnected);
        else {
            TimerTask tt = new TimerTask() {
                public void run(){
                    communication.tryConnect();
                }
            };
            Timer t = new Timer();
            while(!serverConnected){
                t.schedule(tt, 10*1000, 10*1000);
            }
        }
    }


    /**
     *serverListener()调用通讯接口的getString()方法, 等待服务器发送消息, 收到消息
     * 后, 解析消息类型, 然后根据类型调用相关方法, 如添加删除监控项等操作, 执行完这些
     * 方法后会继续调用这个serverListener()方法
     */
    private void serverListener() throws IOException{
        String message;
        int messageType;
        while(shutDownFlag){
            message = communication.getString();
        }
    }

    /**
     * 关闭客户端时, 将shutDownFlag置为false, 停止所有Item监控任务, 并且关闭连接.
     */
    public void shutDown(){
        shutDownFlag = false;
        /*停止所有Item的监控任务*/
        communication.closeConnection();
    }

    /**
     * 启动客户端, 直接读取配置文件
     * @throws javax.xml.parsers.ParserConfigurationException
     * @throws org.xml.sax.SAXException
     * @throws java.io.IOException
     */
    public void start() throws ParserConfigurationException, SAXException, IOException{
        readConfig();
        connectServer();
        createItems();
    }

    private void createItems(){

    }

    private void addItem(Integer itemID, long mPeriod){

    }

    private void deleteItem(Integer itemID){

    }

    private void setItemPeriod(Integer itemID, long mPeriod){

    }
}
